package com.A1908.servlet;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.A1908.entity.Email;
import com.A1908.service.impl.SendMsgServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = {"/sendCaptcha"})
public class SendCaptcha extends HttpServlet {
    SendMsgServiceImpl sendMsgService=new SendMsgServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");

        String opr=req.getParameter("opr");

        switch (opr){
            case "byEmail":
                LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
                String str=lineCaptcha.getImageBase64Data();
                String text="您的验证码是"+lineCaptcha.getCode();
                Email email=new Email();
                email.setEmailText(text);
                if (req.getParameter("tag").equals("getSessionEmail")){
                    email.setSendAddress((String) req.getSession().getAttribute("userEmail"));
                }else {
                    email.setSendAddress(req.getParameter("text"));
                }
                email.setEmailTitle("验证码");
                req.getSession().setAttribute("captcha",lineCaptcha.getCode());
                req.getSession().setMaxInactiveInterval(60);
                sendMsgService.sendByEmail(email);
                break;
            case "byPhone":
                break;
        }
    }
}
